﻿using UnityEngine;
using System.Collections;
using UniLua;

namespace TCG.FFI
{
	/// <summary>
	/// addclip/removeclip/sample未实现.
	/// </summary>

	public static class UnityAnimationLib
	{
		public static string LIB_NAME = "UnityAnimationLib";

		public static string ParentLibName = "UnityBehaviourLib";

		public static int mf_IsAnimatePhysics (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			luaState.PushBoolean (animation.animatePhysics);
			return 1;
		}

		public static int mf_SetAnimatePhysics (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			animation.animatePhysics = luaState.ToBoolean (2);
			return 0;
		}

		public static int mf_GetClip (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			LuaTools.L_PushUserData (luaState, animation.clip, UnityAnimationClipLib.LIB_NAME);
			return 1;
		}

		public static int mf_SetClip (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			AnimationClip clip = LuaTools.L_CheckUData (luaState, 2, UnityAnimationClipLib.LIB_NAME) as AnimationClip;
			animation.clip = clip;
			return 0;
		}

		public static int mf_GetCullingType (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			luaState.PushInteger ((int)animation.cullingType);
			return 1;
		}

		public static int mf_SetCullingType (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			animation.cullingType = (AnimationCullingType)luaState.ToInteger (2);
			return 0;
		}

		public static int mf_IsPlaying (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			if (luaState.GetTop () > 1)
			{
				string aniName = luaState.ToString (2);
				luaState.PushBoolean (animation.IsPlaying (aniName));
			}
			else
			{
				luaState.PushBoolean (animation.isPlaying);
			}
			return 1;
		}

		public static int mf_IsPlayAutomatically (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			luaState.PushBoolean (animation.playAutomatically);
			return 1;
		}

		public static int mf_SetPlayAutomatically (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			animation.playAutomatically = luaState.ToBoolean (2);
			return 0;
		}

		public static int mf___index (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			string stateName = luaState.ToString (2);
			AnimationState state = animation[stateName];
			if (state == null)
			{
				luaState.PushNil ();
			}
			else
			{
				LuaTools.L_PushUserData (luaState, state, UnityAnimationStateLib.LIB_NAME);
			}

			return 1;
		}

		public static int mf_GetWrapMode (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			luaState.PushInteger ((int)animation.wrapMode);
			return 1;
		}

		public static int mf_SetWrapMode (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			animation.wrapMode = (WrapMode) luaState.ToInteger (2);
			return 0;
		}

		public static int mf_Blend (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			string aniName = luaState.ToString (2);
			float targetWeight = 1.0f, fadeLength = 0.3f;
			if (luaState.GetTop () > 2)
			{
				targetWeight = (float)luaState.ToNumber (3);
			}
			if (luaState.GetTop () > 3)
			{
				fadeLength = (float)luaState.ToNumber (4);
			}
			animation.Blend (aniName, targetWeight, fadeLength);

			return 0;
		}

		public static int mf_CrossFade (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			string aniName = luaState.ToString (2);
			float fadeLength = 0.3f;
			PlayMode mode = PlayMode.StopSameLayer;
			if (luaState.GetTop () > 2)
			{
				fadeLength = (float)luaState.ToNumber (3);
			}
			if (luaState.GetTop () > 3)
			{
				mode = (PlayMode) luaState.ToInteger (4);
			}
			animation.CrossFade (aniName, fadeLength, mode);
			return 0;
		}

		public static int mf_CrossFadeQueued (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			string aniName = luaState.ToString (2);
			float fadeLength = 0.3f;
			QueueMode queueMode = QueueMode.CompleteOthers;
			PlayMode playMode = PlayMode.StopSameLayer;
			if (luaState.GetTop () > 2)
			{
				fadeLength =(float)luaState.ToNumber (3);
			}
			if (luaState.GetTop () > 3)
			{
				queueMode = (QueueMode)luaState.ToInteger (4);
			}
			if (luaState.GetTop () > 4)
			{
				playMode = (PlayMode)luaState.ToInteger (5);
			}
			animation.CrossFadeQueued (aniName, fadeLength, queueMode, playMode);
			return 0;
		}

		public static int mf_GetClipCount (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			luaState.PushInteger (animation.GetClipCount ());
			return 1;
		}

		public static int mf_Play (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			if (luaState.GetTop () == 1)
			{
				animation.Play ();
			}
			else if (luaState.GetTop () > 1)
			{
				LuaType paramType = luaState.Type (2);
				if (paramType == LuaType.LUA_TSTRING)
				{
					string aniName = luaState.ToString (2);
					PlayMode playMode = PlayMode.StopSameLayer;
					if (luaState.GetTop () > 2)
					{
						playMode = (PlayMode)luaState.ToInteger (3);
						animation.Play (aniName, playMode);
					}
				}
				else
				{
					PlayMode playMode = (PlayMode)luaState.ToInteger (2);
					animation.Play (playMode);
				}
			}
			return 0;
		}

		public static int mf_PlayQueued (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			string aniName = luaState.ToString (2);
			QueueMode queueMode = QueueMode.CompleteOthers;
			PlayMode playMode = PlayMode.StopSameLayer;
			if (luaState.GetTop () > 2)
			{
				queueMode = (QueueMode)luaState.ToInteger (3);
			}
			if (luaState.GetTop () > 3)
			{
				playMode = (PlayMode)luaState.ToInteger (4);
			}
			animation.PlayQueued (aniName, queueMode, playMode);
			return 0;
		}

		public static int mf_Rewind (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			if (luaState.GetTop () > 1)
			{
				animation.Rewind (luaState.ToString (2));
			}
			else
			{
				animation.Rewind ();
			}
			return 0;
		}

		public static int mf_Stop (ILuaState luaState)
		{
			Animation animation = LuaTools.L_CheckUData (luaState, 1, LIB_NAME) as Animation;
			if (luaState.GetTop () > 1)
			{
				animation.Stop (luaState.ToString (2));
			}
			else
			{
				animation.Stop ();
			}

			return 0;
		}
	}
}
